package com.htl.exampro.user.util;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.File;

@Slf4j
@Component
public class EmailUtil {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.from}")
    private String from;

    public void sendText(String to, String subject, String text) {
        sendText(from, to, subject, text);
    }

    public void sendWithInlineResource(String to, String subject, String htmlContent, String resourceId,
                                       File resourceFile) throws MessagingException {
        sendWithInlineResource(from, to, subject, htmlContent, resourceId, resourceFile);
    }

    @Async
    public void sendHtml(String to, String subject, String htmlContent, File... attachments)
            throws MessagingException {
        sendHtml(from, to, subject, htmlContent, attachments);
    }

    // 同步发送纯文本邮件
    public void sendText(String from, String to, String subject, String text) {
        log.info("Sending email from {} to {}, subject:{},text:{}", from, to, subject, text);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
    }

    // 异步发送HTML邮件（带附件）
    @Async
    public void sendHtml(String from, String to, String subject, String htmlContent, File... attachments)
            throws MessagingException {
        StringBuilder fileNames = new StringBuilder();
        for (File attachment : attachments)
            fileNames.append(":").append(attachment.getName());
        log.info("Sending email from {} to {}, subject:{},text:{},attachments:{}", from, to, subject, htmlContent, fileNames.toString());
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(htmlContent, true);

        for (File file : attachments) {
            helper.addAttachment(file.getName(), new FileSystemResource(file));
        }
        mailSender.send(message);
    }


    // 带内联资源的HTML邮件（如图片）
    public void sendWithInlineResource(String from, String to, String subject,
                                       String htmlContent, String resourceId,
                                       File resourceFile) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(htmlContent, true);
        helper.addInline(resourceId, new FileSystemResource(resourceFile));
        mailSender.send(message);
    }
}
